package com.example.pattern.composite;


import java.util.*;

public class CompositeTest {

    public static void main(String[] args) {

        Person person = new Person("Phen", 24, 1);
        person.addPerson(new Person("Ryan", 28, 1));
        Person thor = new Person("Thor", 29, 1);
        thor.addPerson(new Person("wenju", 26, 0));
        thor.addPerson(new Person("jiazhen", 26, 0));
        person.addPerson(thor);
        System.out.println(person);
        System.out.println("我的朋友：");
        person.getFriends().forEach(p -> {
            System.out.println(p);
            if(p.getFriends().size() > 0){
                System.out.println(p.getName() + "的朋友：");
                p.getFriends().forEach(System.out::println);
            }
        });
    }
}

class Person{

    private String name;
    private int age;
    private int gender;
    private List<Person> friends = new ArrayList<>();

    public Person(String name, int age, int gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void addPerson(Person person){
        this.friends.add(person);
    }

    public void delPerson(Person person){
        this.friends.remove(person);
    }

    public List<Person> getFriends() {
        return friends;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", gender=" + gender +
                '}';
    }
}
